package com.cloud.bssp.admin.wanda.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bssp.admin.wanda.entity.PostDO;
import com.cloud.bssp.admin.wanda.dto.PostDTO;
import com.cloud.bssp.admin.wanda.service.PostService;
import com.cloud.bssp.admin.wanda.convert.PostDoConvert;
import com.cloud.bssp.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: 职务
 * Create Date: 2021-07-04T19:00:10.249
 * Modified By：<br>
 * Modified Date：<br>
 * Why & What is modified：<br>
 *
 * @author weirx
 * @version 1.0
 */
@Api(tags = "职务")
@RestController
@RequestMapping("/post")
@AllArgsConstructor
public class PostController {

    /**
     * PostService
     */
    private PostService postService;

    /**
     * 分页列表
     * @param postDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public R pageList(@RequestBody PostDTO postDTO) {
        QueryWrapper<PostDO> queryWrapper = new QueryWrapper<>(PostDoConvert.dtoToDo(postDTO));
        Page<PostDO> page = new Page<>(postDTO.getPage(), postDTO.getLimit());
        Page<PostDO> pageList = postService.page(page, queryWrapper);
        return R.success(PostDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     * @param postDTO
     * @return
     */
    @ApiOperation(value = "list列表")
    @PostMapping("/list")
    public R list(@RequestBody PostDTO postDTO) {
        QueryWrapper<PostDO> queryWrapper = new QueryWrapper<>(PostDoConvert.dtoToDo(postDTO));
        List<PostDO> postList = postService.list(queryWrapper);
        return R.success(PostDoConvert.listConvert(postList));
    }

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public R info(@RequestParam("id")Long id) {
            PostDO post = postService.getById(id);
        return R.success(PostDoConvert.doToDto(post));
    }

    /**
     * 新增
     * @param postDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public R save(@RequestBody PostDTO postDTO) {
        boolean flag = postService.save(PostDoConvert.dtoToDo(postDTO));
        if (flag) {
            return R.success();
        }
        return R.failed();
    }

    /**
     * 更新
     * @param postDTO
     * @return
     */
    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public R update(@RequestBody PostDTO postDTO) {
        boolean flag = postService.updateById(PostDoConvert.dtoToDo(postDTO));
        if (flag) {
            return R.success();
        }
        return R.failed();
    }
}